<?php

namespace App\Http\Controllers\Post;

use App\Http\Requests\SearchRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class SearchController extends Controller
{
    public function searchPost(SearchRequest $request){
        $keyword = $request->keyword;
        $result = DB::table('posts')
            ->join('users','users.id','=','posts.user_id')
            ->select('posts.title','users.id as user_id',
                'users.user_name as username','posts.like as like',
                'posts.created_at as createtime','posts.view as view','posts.id as postid')
            ->where('posts.status','=',0)
            ->where('posts.title','like','%'.$keyword.'%')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"对不起，没有相关文章"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }
    }
}
